<template>
  <div class="sysSetting">
    <el-form
      ref="editFormRef"
      :model="formData"
      :rules="rules"
      label-width="200px"
      v-if="userInfo.roleType == 1"
    >
      <el-form-item label="是否开启评论/留言板" prop="openCommentType">
        <div>
          <el-radio-group v-model="formData.openCommentType">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </div>
      </el-form-item>
      <template v-if="formData.openCommentType == 1">
        <el-form-item label="appId" prop="changyanAppId">
          <el-input
            placeholder="appId"
            v-model="formData.changyanAppId"
            :maxlength="50"
            ><template #prefix
              ><span class="iconfont icon-id"></span></template
          ></el-input>
        </el-form-item>
        <el-form-item label="appKey" prop="changyanAppKey">
          <el-input
            placeholder="appKey"
            v-model="formData.changyanAppKey"
            :maxlength="50"
            ><template #prefix
              ><span class="iconfont icon-key"></span></template
          ></el-input>
        </el-form-item>
      </template>

      <el-form-item label="Git Pages" prop="gitPageType">
        <div>
          <el-radio-group v-model="formData.gitPageType">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </div>
      </el-form-item>

      <el-form-item label="下载静态界面">
        <div>
          <a href="javascript:void(0)" @click="createZip()" class="a-link"
            >点击下载</a
          >
        </div>
      </el-form-item>

      <el-form-item label="" prop="">
        <el-button plain color="#409eff" @click="submitForm" size="large"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
    <span v-else style="text-align: center;">暂无权限</span>
  </div>
</template>

<script setup>
import VueCookies from "vue-cookies";
import { getCurrentInstance, ref } from "vue";
const { proxy } = getCurrentInstance();

const userInfo = ref(VueCookies.get("userInfo") || {});


const api = {
  getSysSetting: "/sysSetting/getSysSetting",
  saveSysSetting: "/sysSetting/saveSysSetting",
  createZip: "/sysSetting/createZip",
};

const formData = ref({});

const rules = {
  openCommentType: [{ required: true, message: "必填选项" }],
  changyanAppId: [{ required: true, message: "必填选项" }],
  changyanAppKey: [{ required: true, message: "必填选项" }],
  gitPageType: [{ required: true, message: "必填选项" }],
};

const getSysSetting = async () => {
  let result = await proxy.Request({
    url: api.getSysSetting,
  });
  if (result != null) {
    formData.value = result.data;
  }
};
getSysSetting();

const editFormRef = ref();
const submitForm = () => {
  editFormRef.value.validate(async (valid) => {
    if (!valid) {
      return false;
    }
    let params = formData.value;
    Object.assign(params, {
      showIcp: 0,
      icpDomain: "",
      icpNo: "",
      policeProvince: "",
      policeNo: "",
    });
    console.log(params)
    let result = await proxy.Request({
      url: api.saveSysSetting,
      params: params,
    });
    if (!result) {
      return;
    }
    proxy.Message.success("保存成功");
  });
};

// 下载静态界面
const createZip = async () => {
  let result = await proxy.Request({
    url: api.createZip,
  });
  if (!result) {
    return;
  }
  document.location.href =
    "/api/file/download/" + result.data + "/" + encodeURI("静态界面.zip");
};
</script>

<style scoped lang="scss">
.sysSetting{
  display: flex;
  align-items: center;
  width: 100%;
}


</style>